<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>Literal Real Numbers</title>
<link href="../docs-assets/Breadcrumbs.css" rel="stylesheet" rev="stylesheet" type="text/css">
		<meta name="viewport" content="width=device-width initial-scale=1">
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta http-equiv="Content-Language" content="en-gb">

<link href="../docs-assets/Contents.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Progress.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Navigation.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Fonts.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Base.css" rel="stylesheet" rev="stylesheet" type="text/css">
<script>
MathJax = {
	tex: {
		inlineMath: '$', '$'], ['\\(', '\\)'
	},
	svg: {
		fontCache: 'global'
	}
};
</script>
<script type="text/javascript" id="MathJax-script" async
	src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>

<script>
function togglePopup(material_id) {
  var popup = document.getElementById(material_id);
  popup.classList.toggle("show");
}
</script>

<link href="../docs-assets/Popups.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Colours.css" rel="stylesheet" rev="stylesheet" type="text/css">
		
	</head>
	<body class="commentary-font">
		<nav role="navigation">
		<h1><a href="../index.html"><img src="../docs-assets/Inform.png" height=72> </a></h1>
<ul><li><a href="../index.html">home</a></li>
</ul><h2>Compiler</h2><ul>
<li><a href="../structure.html">structure</a></li>
<li><a href="../inbuildn.html">inbuild</a></li>
<li><a href="../inform7n.html">inform7</a></li>
<li><a href="../intern.html">inter</a></li>
<li><a href="../services.html">services</a></li>
<li><a href="../secrets.html">secrets</a></li>
</ul><h2>Other Tools</h2><ul>
<li><a href="../inblorbn.html">inblorb</a></li>
<li><a href="../inform6.html">inform6</a></li>
<li><a href="../inpolicyn.html">inpolicy</a></li>
</ul><h2>Resources</h2><ul>
<li><a href="../extensions.html">extensions</a></li>
<li><a href="../kits.html">kits</a></li>
</ul><h2>Repository</h2><ul>
<li><a href="https://github.com/ganelson/inform"><img src="../docs-assets/github.png" height=0> github</a></li>
</ul><h2>Related Projects</h2><ul>
<li><a href="https://github.com/ganelson/inweb"><img src="../docs-assets/github.png" height=0> inweb</a></li>
<li><a href="https://github.com/ganelson/intest"><img src="../docs-assets/github.png" height=0> intest</a></li>
</ul>
		</nav>
		<main role="main">
		<!-- Weave of 'Literal Real Numbers' generated by inweb -->
<div class="breadcrumbs">
    <ul class="crumbs"><li><a href="../index.html">Home</a></li><li><a href="../inform7n.html">Inform7</a></li><li><a href="index.html">values</a></li><li><a href="index.html#3">Chapter 3: Literals</a></li><li><b>Literal Real Numbers</b></li></ul></div>
<p class="purpose">To parse real numbers written as decimal expansions, or in scientific or engineering notation.</p>

<p class="commentary firstcommentary"><a id="SP1" class="paragraph-anchor"></a><b>&#167;1. </b>Rvalues for the "real number" kind follow the
<a href="http://www.psc.edu/general/software/packages/ieee/ieee.php" class="external">IEEE-754 single-precision encoding</a>
for floating-point numbers.
</p>

<p class="commentary">\(\pi\) and \(e\) get their own special treatment here, if written in lower case.
But \(\gamma\), always in the bronze medal position among mathematical constants,
misses out.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="function-syntax">&lt;s-literal-real-number&gt;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">_</span><span class="plain-syntax"> </span><span class="identifier-syntax">pi</span><span class="plain-syntax"> |</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">_</span><span class="plain-syntax"> </span><span class="identifier-syntax">e</span><span class="plain-syntax"> |</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">plus</span><span class="plain-syntax"> </span><span class="identifier-syntax">infinity</span><span class="plain-syntax"> |</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">minus</span><span class="plain-syntax"> </span><span class="identifier-syntax">infinity</span><span class="plain-syntax"> |</span>
<span class="plain-syntax">    </span><span class="function-syntax">&lt;literal-real-in-digits&gt;</span>

<span class="function-syntax">&lt;literal-real-in-digits&gt;</span><span class="plain-syntax"> </span><span class="identifier-syntax">internal</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> ((</span><span class="identifier-syntax">Wordings::length</span><span class="plain-syntax">(</span><span class="identifier-syntax">W</span><span class="plain-syntax">) != </span><span class="constant-syntax">1</span><span class="plain-syntax">) &amp;&amp; (</span><span class="identifier-syntax">Wordings::length</span><span class="plain-syntax">(</span><span class="identifier-syntax">W</span><span class="plain-syntax">) != </span><span class="constant-syntax">3</span><span class="plain-syntax">)) </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">inchar32_t</span><span class="plain-syntax"> *</span><span class="identifier-syntax">p</span><span class="plain-syntax"> = </span><span class="identifier-syntax">Lexer::word_raw_text</span><span class="plain-syntax">(</span><span class="identifier-syntax">Wordings::first_wn</span><span class="plain-syntax">(</span><span class="identifier-syntax">W</span><span class="plain-syntax">));</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">p</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">expo</span><span class="plain-syntax">=0; </span><span class="reserved-syntax">double</span><span class="plain-syntax"> </span><span class="identifier-syntax">intv</span><span class="plain-syntax">=0, </span><span class="identifier-syntax">fracv</span><span class="plain-syntax">=0;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">expocount</span><span class="plain-syntax">=0, </span><span class="identifier-syntax">intcount</span><span class="plain-syntax">=0, </span><span class="identifier-syntax">fraccount</span><span class="plain-syntax">=0;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">signbit</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">distinctive</span><span class="plain-syntax"> = </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">; </span><span class="comment-syntax"> as a floating-point rather than integer number</span>

<span class="plain-syntax">        </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">i</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP1_1" class="named-paragraph-link"><span class="named-paragraph">Parse the sign at the front</span><span class="named-paragraph-number">1.1</span></a></span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP1_2" class="named-paragraph-link"><span class="named-paragraph">Parse any digits into intv</span><span class="named-paragraph-number">1.2</span></a></span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP1_3" class="named-paragraph-link"><span class="named-paragraph">Parse a decimal expansion</span><span class="named-paragraph-number">1.3</span></a></span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">intcount</span><span class="plain-syntax"> + </span><span class="identifier-syntax">fraccount</span><span class="plain-syntax"> &gt; </span><span class="constant-syntax">0</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> ((</span><span class="identifier-syntax">Wordings::length</span><span class="plain-syntax">(</span><span class="identifier-syntax">W</span><span class="plain-syntax">) &gt; </span><span class="constant-syntax">1</span><span class="plain-syntax">) || (</span><span class="identifier-syntax">p</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">])) </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP1_4" class="named-paragraph-link"><span class="named-paragraph">Parse an exponent</span><span class="named-paragraph-number">1.4</span></a></span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> ((</span><span class="identifier-syntax">distinctive</span><span class="plain-syntax">) || (</span><a href="3-lrn.html#SP6" class="function-link"><span class="function-syntax">LiteralReals::promote</span></a><span class="plain-syntax">())) {</span>
<span class="plain-syntax">                ==&gt; { </span><a href="3-lrn.html#SP3" class="function-link"><span class="function-syntax">LiteralReals::construct_float</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">signbit</span><span class="plain-syntax">, </span><span class="identifier-syntax">intv</span><span class="plain-syntax">, </span><span class="identifier-syntax">fracv</span><span class="plain-syntax">, </span><span class="identifier-syntax">expo</span><span class="plain-syntax">), - };</span>
<span class="plain-syntax">                </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">TRUE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            }</span>
<span class="plain-syntax">        }</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    ==&gt; { </span><span class="identifier-syntax">fail</span><span class="plain-syntax"> </span><span class="identifier-syntax">nonterminal</span><span class="plain-syntax"> };</span>
<span class="plain-syntax">}</span>
</pre>
<p class="commentary firstcommentary"><a id="SP1_1" class="paragraph-anchor"></a><b>&#167;1.1. </b><span class="named-paragraph-container code-font"><span class="named-paragraph-defn">Parse the sign at the front</span><span class="named-paragraph-number">1.1</span></span><span class="comment-syntax"> =</span>
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">p</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">] == </span><span class="character-syntax">'-'</span><span class="plain-syntax">) { </span><span class="identifier-syntax">signbit</span><span class="plain-syntax"> = </span><span class="constant-syntax">1</span><span class="plain-syntax">; </span><span class="identifier-syntax">i</span><span class="plain-syntax">++; }</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">else</span><span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">p</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">] == </span><span class="character-syntax">'+'</span><span class="plain-syntax">) { </span><span class="identifier-syntax">signbit</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">; </span><span class="identifier-syntax">i</span><span class="plain-syntax">++; }</span>
</pre>
<ul class="endnotetexts"><li>This code is used in <a href="3-lrn.html#SP1">&#167;1</a>.</li></ul>
<p class="commentary firstcommentary"><a id="SP1_2" class="paragraph-anchor"></a><b>&#167;1.2. </b><span class="named-paragraph-container code-font"><span class="named-paragraph-defn">Parse any digits into intv</span><span class="named-paragraph-number">1.2</span></span><span class="comment-syntax"> =</span>
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">    </span><span class="reserved-syntax">while</span><span class="plain-syntax"> (</span><span class="identifier-syntax">Characters::isdigit</span><span class="plain-syntax">(</span><span class="identifier-syntax">p</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">])) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">intv</span><span class="plain-syntax"> = </span><span class="constant-syntax">10</span><span class="plain-syntax">.0*</span><span class="identifier-syntax">intv</span><span class="plain-syntax"> + (</span><span class="identifier-syntax">p</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">] - </span><span class="character-syntax">'0'</span><span class="plain-syntax">);</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">intcount</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">i</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">    }</span>
</pre>
<ul class="endnotetexts"><li>This code is used in <a href="3-lrn.html#SP1">&#167;1</a>.</li></ul>
<p class="commentary firstcommentary"><a id="SP1_3" class="paragraph-anchor"></a><b>&#167;1.3. </b><span class="named-paragraph-container code-font"><span class="named-paragraph-defn">Parse a decimal expansion</span><span class="named-paragraph-number">1.3</span></span><span class="comment-syntax"> =</span>
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">p</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">] == </span><span class="character-syntax">'.'</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">distinctive</span><span class="plain-syntax"> = </span><span class="identifier-syntax">TRUE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">i</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">double</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracpow</span><span class="plain-syntax"> = </span><span class="constant-syntax">1</span><span class="plain-syntax">.0;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">while</span><span class="plain-syntax"> (</span><span class="identifier-syntax">Characters::isdigit</span><span class="plain-syntax">(</span><span class="identifier-syntax">p</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">])) {</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">fracpow</span><span class="plain-syntax"> *= </span><span class="constant-syntax">0</span><span class="plain-syntax">.1;</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">fracv</span><span class="plain-syntax"> = </span><span class="identifier-syntax">fracv</span><span class="plain-syntax"> + </span><span class="identifier-syntax">fracpow</span><span class="plain-syntax">*(</span><span class="identifier-syntax">p</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">] - </span><span class="character-syntax">'0'</span><span class="plain-syntax">);</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">fraccount</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">i</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">        }</span>
<span class="plain-syntax">    }</span>
</pre>
<ul class="endnotetexts"><li>This code is used in <a href="3-lrn.html#SP1">&#167;1</a>.</li></ul>
<p class="commentary firstcommentary"><a id="SP1_4" class="paragraph-anchor"></a><b>&#167;1.4. </b><span class="named-paragraph-container code-font"><span class="named-paragraph-defn">Parse an exponent</span><span class="named-paragraph-number">1.4</span></span><span class="comment-syntax"> =</span>
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">    </span><span class="identifier-syntax">inchar32_t</span><span class="plain-syntax"> *</span><span class="identifier-syntax">q</span><span class="plain-syntax"> = </span><span class="identifier-syntax">p</span><span class="plain-syntax"> + </span><span class="identifier-syntax">i</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">e_notation_used</span><span class="plain-syntax"> = </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">Wordings::length</span><span class="plain-syntax">(</span><span class="identifier-syntax">W</span><span class="plain-syntax">) &gt; </span><span class="constant-syntax">1</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">q</span><span class="plain-syntax">[0] != </span><span class="constant-syntax">0</span><span class="plain-syntax">) </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">q</span><span class="plain-syntax"> = </span><span class="identifier-syntax">Lexer::word_raw_text</span><span class="plain-syntax">(</span><span class="identifier-syntax">Wordings::first_wn</span><span class="plain-syntax">(</span><span class="identifier-syntax">W</span><span class="plain-syntax">) + </span><span class="constant-syntax">1</span><span class="plain-syntax">);</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (!((</span><a href="3-lrn.html#SP2" class="function-link"><span class="function-syntax">LiteralReals::ismultiplicationsign</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">q</span><span class="plain-syntax">[0])) &amp;&amp; (</span><span class="identifier-syntax">q</span><span class="plain-syntax">[1] == </span><span class="constant-syntax">0</span><span class="plain-syntax">))) </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">q</span><span class="plain-syntax"> = </span><span class="identifier-syntax">Lexer::word_raw_text</span><span class="plain-syntax">(</span><span class="identifier-syntax">Wordings::first_wn</span><span class="plain-syntax">(</span><span class="identifier-syntax">W</span><span class="plain-syntax">) + </span><span class="constant-syntax">2</span><span class="plain-syntax">);</span>
<span class="plain-syntax">    } </span><span class="reserved-syntax">else</span><span class="plain-syntax"> {</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> ((</span><span class="identifier-syntax">fraccount</span><span class="plain-syntax"> &gt; </span><span class="constant-syntax">0</span><span class="plain-syntax">) &amp;&amp; ((</span><span class="identifier-syntax">q</span><span class="plain-syntax">[0] == </span><span class="character-syntax">'e'</span><span class="plain-syntax">) || (</span><span class="identifier-syntax">q</span><span class="plain-syntax">[0] == </span><span class="character-syntax">'E'</span><span class="plain-syntax">))) </span><span class="identifier-syntax">e_notation_used</span><span class="plain-syntax"> = </span><span class="identifier-syntax">TRUE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">else</span><span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (!(</span><a href="3-lrn.html#SP2" class="function-link"><span class="function-syntax">LiteralReals::ismultiplicationsign</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">q</span><span class="plain-syntax">[0]))) </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">q</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">e_notation_used</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">i</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    } </span><span class="reserved-syntax">else</span><span class="plain-syntax"> {</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (!((</span><span class="identifier-syntax">q</span><span class="plain-syntax">[0] == </span><span class="character-syntax">'1'</span><span class="plain-syntax">) &amp;&amp; (</span><span class="identifier-syntax">q</span><span class="plain-syntax">[1] == </span><span class="character-syntax">'0'</span><span class="plain-syntax">) &amp;&amp; (</span><span class="identifier-syntax">q</span><span class="plain-syntax">[2] == </span><span class="character-syntax">'^'</span><span class="plain-syntax">))) </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">i</span><span class="plain-syntax"> = </span><span class="constant-syntax">3</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">exposign</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">q</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">] == </span><span class="character-syntax">'+'</span><span class="plain-syntax">) </span><span class="identifier-syntax">i</span><span class="plain-syntax">++; </span><span class="reserved-syntax">else</span><span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">q</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">] == </span><span class="character-syntax">'-'</span><span class="plain-syntax">) { </span><span class="identifier-syntax">exposign</span><span class="plain-syntax"> = </span><span class="constant-syntax">1</span><span class="plain-syntax">; </span><span class="identifier-syntax">i</span><span class="plain-syntax">++; }</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">while</span><span class="plain-syntax"> (</span><span class="identifier-syntax">Characters::isdigit</span><span class="plain-syntax">(</span><span class="identifier-syntax">q</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">])) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> = </span><span class="constant-syntax">10</span><span class="plain-syntax">*</span><span class="identifier-syntax">expo</span><span class="plain-syntax"> + (</span><span class="reserved-syntax">int</span><span class="plain-syntax">) (</span><span class="identifier-syntax">q</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">] - </span><span class="character-syntax">'0'</span><span class="plain-syntax">);</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">expocount</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">i</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">q</span><span class="plain-syntax">[</span><span class="identifier-syntax">i</span><span class="plain-syntax">]) </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">expocount</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax">) </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">exposign</span><span class="plain-syntax">) { </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> = -</span><span class="identifier-syntax">expo</span><span class="plain-syntax">; }</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">distinctive</span><span class="plain-syntax"> = </span><span class="identifier-syntax">TRUE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> ((</span><span class="identifier-syntax">e_notation_used</span><span class="plain-syntax">) &amp;&amp;</span>
<span class="plain-syntax">        (</span><span class="identifier-syntax">global_compilation_settings</span><span class="plain-syntax">.</span><span class="identifier-syntax">allow_engineering_notation</span><span class="plain-syntax"> == </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">))</span>
<span class="plain-syntax">        </span><a href="3-lrn.html#SP5" class="function-link"><span class="function-syntax">LiteralReals::wrong_notation</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">W</span><span class="plain-syntax">);</span>
</pre>
<ul class="endnotetexts"><li>This code is used in <a href="3-lrn.html#SP1">&#167;1</a>.</li></ul>
<p class="commentary firstcommentary"><a id="SP2" class="paragraph-anchor"></a><b>&#167;2. </b></p>

<pre class="displayed-code all-displayed-code code-font">
<span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="function-syntax">LiteralReals::ismultiplicationsign</span><button class="popup" onclick="togglePopup('usagePopup1')"><span class="comment-syntax">?</span><span class="popuptext" id="usagePopup1">Usage of <span class="code-font"><span class="function-syntax">LiteralReals::ismultiplicationsign</span></span>:<br/><a href="3-lrn.html#SP1_4">&#167;1.4</a><br/>Literal Patterns - <a href="3-lp.html#SP18_1_3_2">&#167;18.1.3.2</a></span></button><span class="plain-syntax">(</span><span class="identifier-syntax">inchar32_t</span><span class="plain-syntax"> </span><span class="identifier-syntax">c</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> ((</span><span class="identifier-syntax">c</span><span class="plain-syntax"> == </span><span class="character-syntax">'x'</span><span class="plain-syntax">) || (</span><span class="identifier-syntax">c</span><span class="plain-syntax"> == </span><span class="character-syntax">'*'</span><span class="plain-syntax">)) </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">TRUE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">}</span>
</pre>
<p class="commentary firstcommentary"><a id="SP3" class="paragraph-anchor"></a><b>&#167;3. </b>The following function, adapted from code originally written for the Inform 6
compiler by Andrew Plotkin, returns the
<a href="http://www.psc.edu/general/software/packages/ieee/ieee.php" class="external">IEEE-754 single-precision encoding</a>
of a floating-point number.
</p>

<p class="commentary">If the magnitude is too large (beyond about <span class="extract"><span class="extract-syntax">3.4e+38</span></span>), this returns plus or
minus infinity; if the magnitude is too small (below about <span class="extract"><span class="extract-syntax">1e-45</span></span>), this returns
a zero value. If any of the inputs are NaN, this returns NaN.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="function-syntax">LiteralReals::construct_float</span><button class="popup" onclick="togglePopup('usagePopup2')"><span class="comment-syntax">?</span><span class="popuptext" id="usagePopup2">Usage of <span class="code-font"><span class="function-syntax">LiteralReals::construct_float</span></span>:<br/><a href="3-lrn.html#SP1">&#167;1</a><br/>Literal Patterns - <a href="3-lp.html#SP18_1_3_2">&#167;18.1.3.2</a></span></button><span class="plain-syntax">(</span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">signbit</span><span class="plain-syntax">, </span><span class="reserved-syntax">double</span><span class="plain-syntax"> </span><span class="identifier-syntax">intv</span><span class="plain-syntax">, </span><span class="reserved-syntax">double</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracv</span><span class="plain-syntax">, </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">expo</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">double</span><span class="plain-syntax"> </span><span class="identifier-syntax">absval</span><span class="plain-syntax"> = (</span><span class="identifier-syntax">intv</span><span class="plain-syntax"> + </span><span class="identifier-syntax">fracv</span><span class="plain-syntax">) * </span><a href="3-lrn.html#SP4" class="function-link"><span class="function-syntax">LiteralReals::ten_to_the</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">expo</span><span class="plain-syntax">);</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">sign</span><span class="plain-syntax"> = (</span><span class="identifier-syntax">signbit</span><span class="plain-syntax"> ? ((</span><span class="reserved-syntax">int</span><span class="plain-syntax">) </span><span class="constant-syntax">0x80000000</span><span class="plain-syntax">) : </span><span class="constant-syntax">0x0</span><span class="plain-syntax">);</span>

<span class="plain-syntax">    </span><span class="identifier-syntax">latest_constructed_real</span><span class="plain-syntax"> = </span><span class="identifier-syntax">absval</span><span class="plain-syntax">; </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">signbit</span><span class="plain-syntax">) </span><span class="identifier-syntax">latest_constructed_real</span><span class="plain-syntax"> = -</span><span class="identifier-syntax">absval</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">isinf</span><span class="plain-syntax">(</span><span class="identifier-syntax">absval</span><span class="plain-syntax">)) </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP3_5" class="named-paragraph-link"><span class="named-paragraph">Return plus or minus infinity</span><span class="named-paragraph-number">3.5</span></a></span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">isnan</span><span class="plain-syntax">(</span><span class="identifier-syntax">absval</span><span class="plain-syntax">)) </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP3_6" class="named-paragraph-link"><span class="named-paragraph">Return plus or minus Not-a-Number</span><span class="named-paragraph-number">3.6</span></a></span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">double</span><span class="plain-syntax"> </span><span class="identifier-syntax">mant</span><span class="plain-syntax"> = </span><span class="identifier-syntax">frexp</span><span class="plain-syntax">(</span><span class="identifier-syntax">absval</span><span class="plain-syntax">, &amp;</span><span class="identifier-syntax">expo</span><span class="plain-syntax">);</span>
<span class="plain-syntax">    </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP3_1" class="named-paragraph-link"><span class="named-paragraph">Normalize mantissa to be in the range [1.0, 2.0)</span><span class="named-paragraph-number">3.1</span></a></span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">expo</span><span class="plain-syntax"> &gt;= </span><span class="constant-syntax">128</span><span class="plain-syntax">) </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP3_5" class="named-paragraph-link"><span class="named-paragraph">Return plus or minus infinity</span><span class="named-paragraph-number">3.5</span></a></span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">expo</span><span class="plain-syntax"> &lt; -126) </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP3_2" class="named-paragraph-link"><span class="named-paragraph">Denormalize this very small number</span><span class="named-paragraph-number">3.2</span></a></span>
<span class="plain-syntax">    </span><span class="reserved-syntax">else</span><span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (!(</span><span class="identifier-syntax">expo</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax"> &amp;&amp; </span><span class="identifier-syntax">mant</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax">.0)) </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP3_3" class="named-paragraph-link"><span class="named-paragraph">Denormalize this non-zero number</span><span class="named-paragraph-number">3.3</span></a></span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">fbits</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP3_4" class="named-paragraph-link"><span class="named-paragraph">Set fbits to the mantissa times 2 to the 23, rounded to the nearest integer</span><span class="named-paragraph-number">3.4</span></a></span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> (</span><span class="identifier-syntax">sign</span><span class="plain-syntax">) | ((</span><span class="reserved-syntax">int</span><span class="plain-syntax">)(</span><span class="identifier-syntax">expo</span><span class="plain-syntax"> &lt;&lt; </span><span class="constant-syntax">23</span><span class="plain-syntax">)) | (</span><span class="identifier-syntax">fbits</span><span class="plain-syntax">);</span>
<span class="plain-syntax">}</span>
</pre>
<p class="commentary firstcommentary"><a id="SP3_1" class="paragraph-anchor"></a><b>&#167;3.1. </b><span class="named-paragraph-container code-font"><span class="named-paragraph-defn">Normalize mantissa to be in the range [1.0, 2.0)</span><span class="named-paragraph-number">3.1</span></span><span class="comment-syntax"> =</span>
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> ((0.5 &lt;= </span><span class="identifier-syntax">mant</span><span class="plain-syntax">) &amp;&amp; (</span><span class="identifier-syntax">mant</span><span class="plain-syntax"> &lt; </span><span class="constant-syntax">1</span><span class="plain-syntax">.0)) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">mant</span><span class="plain-syntax"> *= </span><span class="constant-syntax">2</span><span class="plain-syntax">.0;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">expo</span><span class="plain-syntax">--;</span>
<span class="plain-syntax">    } </span><span class="reserved-syntax">else</span><span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">mant</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax">.0) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    } </span><span class="reserved-syntax">else</span><span class="plain-syntax"> </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP3_5" class="named-paragraph-link"><span class="named-paragraph">Return plus or minus infinity</span><span class="named-paragraph-number">3.5</span></a></span><span class="plain-syntax">;</span>
</pre>
<ul class="endnotetexts"><li>This code is used in <a href="3-lrn.html#SP3">&#167;3</a>.</li></ul>
<p class="commentary firstcommentary"><a id="SP3_2" class="paragraph-anchor"></a><b>&#167;3.2. </b>One of the following two things then happens, resulting in the exponent
now being in the range 0 to 255, and the mantissa in [0, 1).
</p>

<p class="commentary"><span class="named-paragraph-container code-font"><span class="named-paragraph-defn">Denormalize this very small number</span><span class="named-paragraph-number">3.2</span></span><span class="comment-syntax"> =</span>
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">    </span><span class="identifier-syntax">mant</span><span class="plain-syntax"> = </span><span class="identifier-syntax">ldexp</span><span class="plain-syntax">(</span><span class="identifier-syntax">mant</span><span class="plain-syntax">, </span><span class="constant-syntax">126</span><span class="plain-syntax"> + </span><span class="identifier-syntax">expo</span><span class="plain-syntax">);</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">; </span><span class="comment-syntax"> 0 now represents 10 to the minus 127</span>
</pre>
<ul class="endnotetexts"><li>This code is used in <a href="3-lrn.html#SP3">&#167;3</a>.</li></ul>
<p class="commentary firstcommentary"><a id="SP3_3" class="paragraph-anchor"></a><b>&#167;3.3. </b><span class="named-paragraph-container code-font"><span class="named-paragraph-defn">Denormalize this non-zero number</span><span class="named-paragraph-number">3.3</span></span><span class="comment-syntax"> =</span>
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">    </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> += </span><span class="constant-syntax">127</span><span class="plain-syntax">; </span><span class="comment-syntax"> 127 now represents 10 to the 0, that is, 1</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">mant</span><span class="plain-syntax"> -= </span><span class="constant-syntax">1</span><span class="plain-syntax">.0; </span><span class="comment-syntax"> the mantissa was in the range [1, 2), is now in [0, 1)</span>
</pre>
<ul class="endnotetexts"><li>This code is used in <a href="3-lrn.html#SP3">&#167;3</a>.</li></ul>
<p class="commentary firstcommentary"><a id="SP3_4" class="paragraph-anchor"></a><b>&#167;3.4. </b>At this point the mantissa is a number in the range [0, 1), so multiplying
it by 2 to the 23 will produce the bottom 22 bits of our answer. The case
we have to be wary of is where the carry propagates out of a string of 23
1 bits, that is, where we end up with bit 23 set as well: in that case, the
exponent increments, and we round to that power of 10 exactly.
</p>

<p class="commentary">Note that 2 to the 23 is 8388608.
</p>

<p class="commentary"><span class="named-paragraph-container code-font"><span class="named-paragraph-defn">Set fbits to the mantissa times 2 to the 23, rounded to the nearest integer</span><span class="named-paragraph-number">3.4</span></span><span class="comment-syntax"> =</span>
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">    </span><span class="identifier-syntax">fbits</span><span class="plain-syntax"> = (</span><span class="reserved-syntax">int</span><span class="plain-syntax">) ((</span><span class="identifier-syntax">mant</span><span class="plain-syntax">*8388608.0) + </span><span class="constant-syntax">0</span><span class="plain-syntax">.5); </span><span class="comment-syntax"> round to nearest integer</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">fbits</span><span class="plain-syntax"> &gt;&gt; </span><span class="constant-syntax">23</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">fbits</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">expo</span><span class="plain-syntax">++; </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">expo</span><span class="plain-syntax"> &gt;= </span><span class="constant-syntax">255</span><span class="plain-syntax">) </span><span class="named-paragraph-container code-font"><a href="3-lrn.html#SP3_5" class="named-paragraph-link"><span class="named-paragraph">Return plus or minus infinity</span><span class="named-paragraph-number">3.5</span></a></span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>
</pre>
<ul class="endnotetexts"><li>This code is used in <a href="3-lrn.html#SP3">&#167;3</a>.</li></ul>
<p class="commentary firstcommentary"><a id="SP3_5" class="paragraph-anchor"></a><b>&#167;3.5. </b><span class="named-paragraph-container code-font"><span class="named-paragraph-defn">Return plus or minus infinity</span><span class="named-paragraph-number">3.5</span></span><span class="comment-syntax"> =</span>
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">sign</span><span class="plain-syntax"> | </span><span class="constant-syntax">0x7f800000</span><span class="plain-syntax">;</span>
</pre>
<ul class="endnotetexts"><li>This code is used in <a href="3-lrn.html#SP3">&#167;3</a> (twice), <a href="3-lrn.html#SP3_1">&#167;3.1</a> and <a href="3-lrn.html#SP3_4">&#167;3.4</a>.</li></ul>
<p class="commentary firstcommentary"><a id="SP3_6" class="paragraph-anchor"></a><b>&#167;3.6. </b><span class="named-paragraph-container code-font"><span class="named-paragraph-defn">Return plus or minus Not-a-Number</span><span class="named-paragraph-number">3.6</span></span><span class="comment-syntax"> =</span>
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">sign</span><span class="plain-syntax"> | </span><span class="constant-syntax">0x7fc00000</span><span class="plain-syntax">;</span>
</pre>
<ul class="endnotetexts"><li>This code is used in <a href="3-lrn.html#SP3">&#167;3</a>.</li></ul>
<p class="commentary firstcommentary"><a id="SP4" class="paragraph-anchor"></a><b>&#167;4. </b>The following returns 10 to the given power.
</p>

<pre class="definitions code-font"><span class="definition-keyword">define</span> <span class="constant-syntax">POW10_RANGE</span><span class="plain-syntax"> </span><span class="constant-syntax">8</span>
</pre>
<pre class="displayed-code all-displayed-code code-font">
<span class="reserved-syntax">double</span><span class="plain-syntax"> </span><span class="function-syntax">LiteralReals::ten_to_the</span><button class="popup" onclick="togglePopup('usagePopup3')"><span class="comment-syntax">?</span><span class="popuptext" id="usagePopup3">Usage of <span class="code-font"><span class="function-syntax">LiteralReals::ten_to_the</span></span>:<br/><a href="3-lrn.html#SP3">&#167;3</a></span></button><span class="plain-syntax">(</span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">expo</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">static</span><span class="plain-syntax"> </span><span class="reserved-syntax">double</span><span class="plain-syntax"> </span><span class="identifier-syntax">powers</span><span class="plain-syntax">[</span><span class="constant-syntax">POW10_RANGE</span><span class="plain-syntax">*2+1] = {</span>
<span class="plain-syntax">        </span><span class="constant-syntax">0</span><span class="plain-syntax">.00000001, </span><span class="constant-syntax">0</span><span class="plain-syntax">.0000001, </span><span class="constant-syntax">0</span><span class="plain-syntax">.000001, </span><span class="constant-syntax">0</span><span class="plain-syntax">.00001, </span><span class="constant-syntax">0</span><span class="plain-syntax">.0001, </span><span class="constant-syntax">0</span><span class="plain-syntax">.001, </span><span class="constant-syntax">0</span><span class="plain-syntax">.01, </span><span class="constant-syntax">0</span><span class="plain-syntax">.1,</span>
<span class="plain-syntax">        </span><span class="constant-syntax">1</span><span class="plain-syntax">.0,</span>
<span class="plain-syntax">        </span><span class="constant-syntax">10</span><span class="plain-syntax">.0, </span><span class="constant-syntax">100</span><span class="plain-syntax">.0, </span><span class="constant-syntax">1000</span><span class="plain-syntax">.0, </span><span class="constant-syntax">10000</span><span class="plain-syntax">.0, </span><span class="constant-syntax">100000</span><span class="plain-syntax">.0, </span><span class="constant-syntax">1000000</span><span class="plain-syntax">.0, </span><span class="constant-syntax">10000000</span><span class="plain-syntax">.0, </span><span class="constant-syntax">100000000</span><span class="plain-syntax">.0</span>
<span class="plain-syntax">    };</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">double</span><span class="plain-syntax"> </span><span class="identifier-syntax">res</span><span class="plain-syntax"> = </span><span class="constant-syntax">1</span><span class="plain-syntax">.0;</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">expo</span><span class="plain-syntax"> &lt; </span><span class="constant-syntax">0</span><span class="plain-syntax">)</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">for</span><span class="plain-syntax"> (; </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> &lt; -</span><span class="constant-syntax">POW10_RANGE</span><span class="plain-syntax">; </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> += </span><span class="constant-syntax">POW10_RANGE</span><span class="plain-syntax">)</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">res</span><span class="plain-syntax"> *= </span><span class="identifier-syntax">powers</span><span class="plain-syntax">[0];</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">else</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">for</span><span class="plain-syntax"> (; </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> &gt; </span><span class="constant-syntax">POW10_RANGE</span><span class="plain-syntax">; </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> -= </span><span class="constant-syntax">POW10_RANGE</span><span class="plain-syntax">)</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">res</span><span class="plain-syntax"> *= </span><span class="identifier-syntax">powers</span><span class="plain-syntax">[</span><span class="constant-syntax">POW10_RANGE</span><span class="plain-syntax">*2];</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">res</span><span class="plain-syntax"> * </span><span class="identifier-syntax">powers</span><span class="plain-syntax">[</span><span class="constant-syntax">POW10_RANGE</span><span class="plain-syntax">+</span><span class="identifier-syntax">expo</span><span class="plain-syntax">];</span>
<span class="plain-syntax">}</span>
</pre>
<p class="commentary firstcommentary"><a id="SP5" class="paragraph-anchor"></a><b>&#167;5. </b>The problem message for engineering notation should only appear once:
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">e_notation_problem_issued</span><span class="plain-syntax"> = </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>

<span class="reserved-syntax">void</span><span class="plain-syntax"> </span><span class="function-syntax">LiteralReals::wrong_notation</span><button class="popup" onclick="togglePopup('usagePopup4')"><span class="comment-syntax">?</span><span class="popuptext" id="usagePopup4">Usage of <span class="code-font"><span class="function-syntax">LiteralReals::wrong_notation</span></span>:<br/><a href="3-lrn.html#SP1_4">&#167;1.4</a></span></button><span class="plain-syntax">(</span><span class="identifier-syntax">wording</span><span class="plain-syntax"> </span><span class="identifier-syntax">W</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">e_notation_problem_issued</span><span class="plain-syntax"> == </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">e_notation_problem_issued</span><span class="plain-syntax"> = </span><span class="identifier-syntax">TRUE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">Problems::quote_source</span><span class="plain-syntax">(1, </span><span class="identifier-syntax">current_sentence</span><span class="plain-syntax">);</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">Problems::quote_wording</span><span class="plain-syntax">(2, </span><span class="identifier-syntax">W</span><span class="plain-syntax">);</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">StandardProblems::handmade_problem</span><span class="plain-syntax">(</span><span class="identifier-syntax">Task::syntax_tree</span><span class="plain-syntax">(), </span><span class="identifier-syntax">_p_</span><span class="plain-syntax">(</span><span class="identifier-syntax">PM_WantonEngineering</span><span class="plain-syntax">));</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">Problems::issue_problem_segment</span><span class="plain-syntax">(</span>
<span class="plain-syntax">            </span><span class="string-syntax">"In %1, you write '%2', which looks to me like the engineering "</span>
<span class="plain-syntax">            </span><span class="string-syntax">"notation for a real number - I'm guessing that the 'e' means "</span>
<span class="plain-syntax">            </span><span class="string-syntax">"exponent, so for example, 1E+6 means 1000000. Inform writes "</span>
<span class="plain-syntax">            </span><span class="string-syntax">"numbers like this as 1 x 10^6, or if you prefer 1.0 x 10^6; "</span>
<span class="plain-syntax">            </span><span class="string-syntax">"or you can use a multiplication sign instead of the 'x'."</span><span class="plain-syntax">);</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">Problems::issue_problem_end</span><span class="plain-syntax">();</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">}</span>
</pre>
<p class="commentary firstcommentary"><a id="SP6" class="paragraph-anchor"></a><b>&#167;6. </b>This upgrades a literal <span class="extract"><span class="extract-syntax">K_number</span></span> to a literal <span class="extract"><span class="extract-syntax">K_real_number</span></span> if necessary,
as it can be when performing arithmetic. Note that we do this not by running
an integer-to-real conversion inside Inform, but just by re-parsing it in a
real context: the result is therefore identical to what would have been parsed
had the literal in question been expected to be real all along.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">promoting_reals</span><span class="plain-syntax"> = </span><span class="identifier-syntax">FALSE</span><span class="plain-syntax">;</span>
<span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="function-syntax">LiteralReals::promote</span><button class="popup" onclick="togglePopup('usagePopup5')"><span class="comment-syntax">?</span><span class="popuptext" id="usagePopup5">Usage of <span class="code-font"><span class="function-syntax">LiteralReals::promote</span></span>:<br/><a href="3-lrn.html#SP1">&#167;1</a></span></button><span class="plain-syntax">(</span><span class="reserved-syntax">void</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">promoting_reals</span><span class="plain-syntax">;</span>
<span class="plain-syntax">}</span>
<span class="identifier-syntax">parse_node</span><span class="plain-syntax"> *</span><span class="function-syntax">LiteralReals::promote_number_if_necessary</span><span class="plain-syntax">(</span><span class="identifier-syntax">parse_node</span><span class="plain-syntax"> *</span><span class="identifier-syntax">value</span><span class="plain-syntax">, </span><span class="identifier-syntax">kind</span><span class="plain-syntax"> *</span><span class="identifier-syntax">to</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">s</span><span class="plain-syntax"> = </span><span class="identifier-syntax">promoting_reals</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">promoting_reals</span><span class="plain-syntax"> = </span><span class="identifier-syntax">TRUE</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">kind</span><span class="plain-syntax"> *</span><span class="identifier-syntax">from</span><span class="plain-syntax"> = </span><a href="2-spc.html#SP1" class="function-link"><span class="function-syntax">Specifications::to_kind</span></a><span class="plain-syntax">(</span><span class="identifier-syntax">value</span><span class="plain-syntax">);</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> ((</span><span class="identifier-syntax">Kinds::eq</span><span class="plain-syntax">(</span><span class="identifier-syntax">from</span><span class="plain-syntax">, </span><span class="identifier-syntax">K_number</span><span class="plain-syntax">)) &amp;&amp; (</span><span class="identifier-syntax">Kinds::eq</span><span class="plain-syntax">(</span><span class="identifier-syntax">to</span><span class="plain-syntax">, </span><span class="identifier-syntax">K_real_number</span><span class="plain-syntax">))) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">wording</span><span class="plain-syntax"> </span><span class="identifier-syntax">W</span><span class="plain-syntax"> = </span><span class="identifier-syntax">Node::get_text</span><span class="plain-syntax">(</span><span class="identifier-syntax">value</span><span class="plain-syntax">);</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="function-syntax">&lt;s-literal-real-number&gt;</span><span class="plain-syntax">(</span><span class="identifier-syntax">W</span><span class="plain-syntax">)) </span><span class="identifier-syntax">value</span><span class="plain-syntax"> = </span><span class="function-syntax">&lt;&lt;rp&gt;&gt;</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">promoting_reals</span><span class="plain-syntax"> = </span><span class="identifier-syntax">s</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">value</span><span class="plain-syntax">;</span>
<span class="plain-syntax">}</span>
</pre>
<nav role="progress"><div class="progresscontainer">
    <ul class="progressbar"><li class="progressprev"><a href="3-lp.html">&#10094;</a></li><li class="progresschapter"><a href="P-wtmd.html">P</a></li><li class="progresschapter"><a href="1-vm.html">1</a></li><li class="progresschapter"><a href="2-spc.html">2</a></li><li class="progresscurrentchapter">3</li><li class="progresssection"><a href="3-pl.html">pl</a></li><li class="progresssection"><a href="3-lp.html">lp</a></li><li class="progresscurrent">lrn</li><li class="progresssection"><a href="3-lvn.html">lvn</a></li><li class="progresssection"><a href="3-tod.html">tod</a></li><li class="progresssection"><a href="3-ul.html">ul</a></li><li class="progresssection"><a href="3-tt.html">tt</a></li><li class="progresssection"><a href="3-ll.html">ll</a></li><li class="progresschapter"><a href="4-ets.html">4</a></li><li class="progresschapter"><a href="5-dsh.html">5</a></li><li class="progressnext"><a href="3-lvn.html">&#10095;</a></li></ul></div>
</nav><!-- End of weave -->

		</main>
	</body>
</html>

